import json
import urllib

import scrapy
import urllib3


def http_post(data_json):
    jdata = json.dumps(data_json)
    http = urllib3.PoolManager()
    response = http.request('POST', 'http://localhost:15000', headers={'content-type': 'application/json'},
                            fields=jdata)
    return response.read()


class SoftWareSpider(scrapy.Spider):
    name = 'soft_ware'
    allowed_domains = ['cs.nankai.edu.cn']
    # start_urls = ['https://cs.nankai.edu.cn/xydt/yjszs.htm']
    start_urls = ['https://cs.nankai.edu.cn/xydt/yjszs/13.htm']

    def parse(self, response):
        url = None
        for index in range(1, 11):
            data = response.xpath('//*[@id="adminForm"]/table/tbody/tr[{}]/td[1]/a'.format(index))
            if '复试时间' in data.extract_first():
                url = data.extract_first()
                print("URL: {}".format(url))
                break

        if url is not None:
            http_post({
                "target": 877719341,
                "messageChain": [
                    {"type": "Plain", "text": "访问"}
                ]
            })
            with open('send.txt', 'w') as f:
                f.write('sent')
